package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllCureEvtity;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;

/**
 * 病情治疗信息
 * Created by guom on 2018/10/15.
 */
public class IllCureTable {
    public static String tableName = "pat_ill_cures";
    public static String tableIllId = "ill_id";//病例id
    public static String tableSurgeryTime = "surgery_time";//手术时间
    public static String tableSurgeryMode = "surgery_mode";//手术方式
    public static String tableSurgerySide = "surgery_side";//手术侧别
    public static String tableExpectantTreatment = "expectant_treatment";//保守治疗
    public static String tableCureExplain = "cure_explain";//补充说明
    public static String tableCreateTime = "create_time";// 创建时间
    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200), "
            + tableSurgeryTime + " varchar(200), "
            + tableSurgeryMode + " varchar(200), "
            + tableSurgerySide + " varchar(200), "
            + tableExpectantTreatment + " longText, "
            + tableCureExplain + " longText, "
            + tableCreateTime + " datetime "
            + "); ";

    public static boolean onInsert(JdbcTemplate jdbcTemplate, String illId, IllCureEvtity bean) {
        if (bean == null) {
            bean = new IllCureEvtity();
        }
        String sql = "INSERT INTO " + IllCureTable.tableName
                + "("
                + IllCureTable.tableIllId + ","
                + IllCureTable.tableSurgeryTime + ","
                + IllCureTable.tableSurgeryMode + ","
                + IllCureTable.tableSurgerySide + ","
                + IllCureTable.tableExpectantTreatment + ","
                + IllCureTable.tableCureExplain + ","
                + IllCureTable.tableCreateTime
                + ")"
                + " VALUES (?,?,?, ?,?,?, ?)";

        Object[] obj = new Object[]{illId, bean.getSurgeryTime(), bean.getSurgeryMode(),
                bean.getSurgerySide(), bean.getExpectantTreatment(), bean.getCureExplain(),
                new Date()};
        jdbcTemplate.update(sql, obj);
        return true;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllCureTable.tableName
                + " WHERE "
                + IllCureTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllCureEvtity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getSurgeryTime();
        if (value!=null) {
            update += " , " + IllCureTable.tableSurgeryTime + " = '" + value + "'";
        }
        value = bean.getSurgeryMode();
        if (value!=null) {
            update += " , " + IllCureTable.tableSurgeryMode + " = '" + value + "'";
        }
        value = bean.getSurgerySide();
        if (value!=null) {
            update += " , " + IllCureTable.tableSurgerySide + " = '" + value + "'";
        }
        value = bean.getExpectantTreatment();
        if (value!=null) {
            update += " , " + IllCureTable.tableExpectantTreatment + " = '" + value + "'";
        }
        value = bean.getCureExplain();
        if (value!=null) {
            update += " , " + IllCureTable.tableCureExplain + " = '" + value + "'";
        }
        if (update.equals("")) {
            return true;
        }
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllCureTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllCureTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }
    public static IllCureEvtity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllCureTable.tableName
                + " WHERE "
                + IllCureTable.tableIllId + " = '" + illId + "'";
        IllCureEvtity entity = new IllCureEvtity();
        List<IllCureEvtity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }


    private static List<IllCureEvtity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllCureEvtity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllCureEvtity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }
}
